package com.csii.pos.sysparam;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.csii.adhoc.annotation.OperationLog;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.util.ConvertUtils;
import com.csii.adhoc.util.myabtisplus.QueryWrapperUtils;
import com.csii.adhoc.controller.BaseController;
import com.csii.pos.sysparam.service.ISysParamService;
import com.csii.result.APIResult;
import lombok.extern.slf4j.Slf4j;


import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @Description: 系统参数表
 * @Author: power-stdio
 * @Date:   2021-07-06
 * @Version: V1.0
 */

@RestController
@RequestMapping("/sysParam")
@Slf4j
public class SysParamController extends BaseController<SysParam, ISysParamService> {
    @Autowired
    private ISysParamService sysParamService;

    /**
     * 分页列表查询
     *
     * @param sysParamVO 参数
     * @return 分页结构
     */
    @OperationLog(value = "系统参数表-列表查询")
    @PostMapping(value = "/queryParamList")
    public APIResult<?> queryParamList(@RequestBody(required = false) SysParamVO sysParamVO) {
        QueryWrapper<SysParam> queryWrapper=null;
        SysParam sysParam  = ConvertUtils.convert(SysParam.class,sysParamVO);
        if(sysParam!=null){
        queryWrapper = QueryWrapperUtils.getEqQueryWrapperFromDto(sysParam, new QueryWrapper<>());
        }else {
            queryWrapper = new QueryWrapper<>();
        }
        queryWrapper.eq("ACTIVE","0");
        List<SysParam> list = sysParamService.list(queryWrapper);
        return  APIResult.successResult(list);
    }

    /**
     * 分页列表查询
     *
     * @param sysParamVO 参数
     * @return 分页结构
     */
    @OperationLog(value = "系统参数表-分页列表查询")
    @PostMapping(value = "/list")
    public APIResult<?> queryPageList(@RequestBody SysParamVO sysParamVO) {
        SysParam sysParam  = ConvertUtils.convert(SysParam.class,sysParamVO);
        QueryWrapper<SysParam> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(sysParam.getCode())){
            queryWrapper.like("CODE",sysParam.getCode());
        }
        Page<SysParam> page = new Page<SysParam>(sysParamVO.getPageNo(), sysParamVO.getPageSize());
        IPage<SysParam> pageList = sysParamService.page(page, queryWrapper);
        IPage<SysParamVO> pageListVo = ConvertUtils.convertIPage(SysParamVO.class, pageList);
        return  APIResult.successResult(pageListVo);
    }

    /**
     *   添加
     *
     * @param sysParamVO 参数
     * @return  是否成功
     */
    @OperationLog(value = "系统参数表-添加")
    @PostMapping(value = "/add")
    public APIResult<?> add(@RequestBody SysParamVO sysParamVO) {
        SysParam sysParam  = ConvertUtils.convert(SysParam.class,sysParamVO);
        sysParam.setCreatedBy(String.valueOf(SecurityUtils.getSubject().getPrincipal()));
        sysParam.setCreatedDate(new Date());
        sysParamService.save(sysParam);
        return APIResult.successResult("添加成功！");
    }

    /**
     *  编辑
     *
     * @param sysParamVO 参数
     * @return 编辑结果
     */
    @OperationLog(value = "系统参数表-编辑")
    @PostMapping(value = "/edit")
    public APIResult<?> edit(@RequestBody SysParamVO sysParamVO) {
        SysParam sysParam  = ConvertUtils.convert(SysParam.class,sysParamVO);
        sysParamService.updateById(sysParam);
        return APIResult.successResult("编辑成功!");
    }

    /**
     * @description:  输入框查询是提示接口
     * @param: hintVo
     * @return:  模糊查询list
     * @author liuzhonghua
     * @date:
     */
    @RequestMapping("queryHint")
    public APIResult<?> queryHint(@RequestBody SysParamVO sysParamVO) {
        QueryWrapper<SysParam> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT CODE");
        if (StringUtils.isEmpty(sysParamVO.getCode())) {
            queryWrapper.like("CODE", sysParamVO.getCode());
        }
        List<SysParam> list = sysParamService.list(queryWrapper);
        return APIResult.successResult(list);
    }

    /**
     *   通过id删除
     * @param map id
     * @return 删除结构
     */
    @OperationLog(value = "系统参数表-通过id删除")
    @PostMapping(value = "/delete")
    public APIResult<?> delete(@RequestBody Map<String,String> map) {
        sysParamService.removeById(map.get("id"));
        return APIResult.successResult("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids id集合
     * @return 删除结构
     */
    @OperationLog(value = "系统参数表-批量删除")
    @PostMapping(value = "/deleteBatch")
    public APIResult<?> deleteBatch(@RequestBody List<SysParamVO> ids) {
        this.sysParamService.removeByIds(ids.stream().map(SysParamVO::getId).collect(Collectors.toList()));
        return APIResult.successResult("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id id
     * @return 查询结构
     */
    @OperationLog(value = "系统参数表-通过id查询")
    @PostMapping(value = "/queryById")
    public APIResult<?> queryById(@RequestParam(name="id") String id) {
        SysParam sysParam = sysParamService.getById(id);
        if(sysParam==null) {
            return APIResult.failResult(null, "未找到对应数据");
        }
        SysParamVO sysParamVO  = ConvertUtils.convert(SysParamVO.class,sysParam);
        return APIResult.successResult(sysParamVO);
    }

    /**
     * 导出excel
     * @param sysParamVO f
     */
    @RequestMapping(value = "/exportXls")
    public void exportXls(HttpServletResponse response, SysParamVO sysParamVO) {
        SysParam sysParam  = ConvertUtils.convert(SysParam.class,sysParamVO);
        super.exportXls(response,sysParam, SysParam.class, "系统参数表");
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public APIResult<?> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        return APIResult.successResult(super.importExcel(file, SysParam.class));
    }
}
